package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @program: cloud2020
 * @description: TODO
 * @author: YuWenYi
 * @create: 2021-04-13 12:10
 **/
@RestController
@Slf4j
public class OrderController {

    //public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    //浏览器客户端一般只能发送get请求
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> createPayment(Payment payment){
        log.info("客户端发起支付,调用payment微服务");
        //客户端发起get请求,然后到这里来,远程调用(发送post请求)payment微服务的方法
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    //使用postForEntity
    @GetMapping("/consumer/payment/createWithEntity")
    public CommonResult<Payment> createPaymentWithEntity(Payment payment){
        log.info("客户端通过postForEntity发起支付,调用payment微服务");
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PAYMENT_URL + "/payment/create", payment, CommonResult.class);

        log.info("响应头信息为:"+entity.getHeaders());

        return entity.getBody();
    }


    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        log.info("客户端查询支付,调用payment微服务");
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

    //使用getForEntity
    //它返回的是ResponseEntity对象,包含了相应的一些重要信息,比如响应头,相应状态码,响应体等
    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPaymentWithEntityById(@PathVariable("id") Long id){
        log.info("客户端使用getForEntity查询支付,调用payment微服务");
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            return entity.getBody();
        }else {
            return new CommonResult<>(444,"服务调用失败!");
        }
    }

    //注入自定义接口实现类
    @Autowired
    private LoadBalancer loadBalancer;

    @Autowired
    private DiscoveryClient discoveryClient;

    //使用自定义的负载均衡算法
    @GetMapping(value = "/consumer/payment/lb")
    public String getPaymentLB() {
        //获取指定服务下的所有运行实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");

        if (instances == null || instances.size()<=0){
            return null;
        }

        //经过轮询算法之后,获取要访问的服务器实例
        ServiceInstance serviceInstance = loadBalancer.instances(instances);

        //通过服务器实例,获取该实例的访问地址!  --> http://localhost:8001 / 8002
        URI uri = serviceInstance.getUri();

        System.out.println("---->"+uri.toString());

        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }

    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        return restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
    }


}
